//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by 曹曹 on 16/7/18.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 手势识别器有7种
    // UIGestureRecognizer作为所有手势的基类(父类)
#pragma mark - 轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
    // 点击几次触发, 默认为1
    tap.numberOfTapsRequired = 1;
    // 几根手指触发(option模拟两根手指) 默认为1
    tap.numberOfTouchesRequired = 1;
#pragma mark - 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.view addGestureRecognizer:longPress];
    //
    longPress.minimumPressDuration = 1;
    // 默认10像素, 长按失败前最大的移动距离
    longPress.allowableMovement = 10;
#pragma mark - 轻扫
//    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//    [self.view addGestureRecognizer:swipe];
//    swipe.numberOfTouchesRequired = 1;
    // 方向, 默认是UISwipeGestureRecognizerDirectionRight
    // 可以定义多个方向, 前提是多个方向触发的行为相同
    // 如果多个方向触发的行为不同, 则需要定义多个轻扫手势
    // 左右 和 上下有冲突
//    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
#pragma mark - 轻扫切换imageView的图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 300)];
    imageView.image = [UIImage imageNamed:@"1.jpg"];
//    [self.view addSubview:imageView];
    // 将图片的交互打开(UIImageView和UILabel默认是关闭的)
    imageView.userInteractionEnabled = YES;
    // 向左轻扫手势
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
//    [imageView addGestureRecognizer:swipeLeft];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    // 向右轻扫手势
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
//    [imageView addGestureRecognizer:swipeRight];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    // 按位枚举, 4个选项 → 1 1 1 1
    // 全选的情况 1(3) 1 (2) 1(1) 1(0)
#pragma mark - 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [imageView addGestureRecognizer:pan];
#pragma mark - 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [imageView addGestureRecognizer:pinch];
#pragma mark - 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [imageView addGestureRecognizer:rotation];
#pragma mark - 屏幕边缘(快速返回手势)
    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan:)];
     edgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePan];
}
- (void)tap:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"轻扫");
}
- (void)swipeImage:(UISwipeGestureRecognizer *)swipeImage {
    UIImageView *imageView = (UIImageView *)swipeImage.view;
    NSString *name = [NSString stringWithFormat:@"%u.jpg", (arc4random() % (6 - 1 + 1) + 1)];
    switch (swipeImage.direction) {
        case UISwipeGestureRecognizerDirectionLeft:imageView.image = [UIImage imageNamed:name];break;
        case UISwipeGestureRecognizerDirectionRight:imageView.image = [UIImage imageNamed:name];break;
        default:break;
    }
#pragma mark - 按位枚举的判断条件
    if (swipeImage.direction & UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"左划");
    } else if (swipeImage.direction & UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"右划");
    }
}
- (void)pan:(UIPanGestureRecognizer *)pan {
#pragma mark - 以手势的状态为判断条件
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        // 平移中
        NSLog(@"平移ing");
        // 平移手势, 根据其他view上移动的过程, 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        NSLog(@"%@", NSStringFromCGPoint(point));
        // 根据point使pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan移动的点置空
        [pan setTranslation:CGPointZero inView:pan.view];
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        // 平移结束(到初始位置)
        pan.view.transform = CGAffineTransformIdentity;
    }
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
    NSLog(@"%f", (CGFloat)pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 设置缩放倍数为1
    pinch.scale = 1;
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    // 旋转弧度置为0, 旋转速度变慢
    rotation.rotation = 0;
}
- (void)edgePan:(UIScreenEdgePanGestureRecognizer *)edgePan {
    NSLog(@"屏幕边缘");
}
#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
